<?php
/**
 * File for the TIS class
 * 
 * $Rev:: 44            $:  Revision of last commit
 * $Author:: guy        $:  Author of last commit
 * $Date:: 2010-03-01 1#$:  Date of last commit
 * 
 * @package eesc
 */

/**
 * Require rules
 */
require_once BASE_URI . 'inc/config.inc.php';

/**
 * Class for retrieving info from the existing TIS
 * 
 * 
 * Main use is the 'Select' method.  But we also have other
 * common needs
 * 
 * 
 * @package eesc
 * @subpackage classes
 */
class TIS {
	
	/**
	 * @var $db database connection variable
	 */
	private $db;
	
	
	/**
	 * Constructor
	 * 
	 * Build up the connection to the database 
	 */
	public function __construct() {

		putenv("ODBCINSTINI=/Library/ODBC/odbcinst.ini");
		putenv("ODBCINI=/Library/ODBC/odbc.ini");
		// set up variables for the connection parameters
		$dsn = "Teaching"; // this DSN must be configured as a System DSN
		$userID = ""; // set this to whatever user ID you use to connect
		$password = "";
		
		$this->db = odbc_connect("$dsn", "$userID", "$password");
		if (!$this->db) {
			trigger_error("Connect failed:" . odbc_errormsg());
		    exit();
		}
	}
	
	
	/**
	 * Select function to gather a result from the TIS
	 * 
	 * @param String $table DB's table name
	 * @param object $columns [optional]
	 * @param String $extra [optional] Extras on the query line
	 * @return array $data resultset rows
	 */
	public function select($table, $columns = "*", $extra = NULL) {
				
		if (is_string($columns)) {
			$columns = explode(",", $columns);
		}

		$query = "SELECT " . implode(", ", $columns);
		$query .= " FROM ". $table;
		if (isset($extra)) {
			$query .= " ".$extra;
		}
		$query .= ";";
		
		$result = odbc_exec($this->db, $query);
		$data = array();
		while ($row = odbc_fetch_array($result)) {
		    $data[] = $row;
		}
		
		return $data;
	}
	
	/**
	 * Retrieve a list of the courses/papers
	 * 
	 * @param object $tripos [optional]
	 * @return $papers array of the courses
	 */
	public function getCourses($tripos = NULL) {
		/*if (isset($tripos)) {
			$result = $this->select("tbl_courses","*","WHERE L_INT_TriposGroupID = ". $tripos);
		} else {
			$result = $this->select("tbl_courses");
		}*/
		
		$this->mysql = new Database();
		
		$this->mysql->connect();
		if (isset($tripos)) {		
			$data = $this->mysql->select("papers", array("ID as A_INT_CourseID","title as STR_SubjectName","triposGroupID as TXT_CourseURL") , "WHERE TriposGroupID = " . $tripos);
		} else {
			$data = $this->mysql->select("papers", array("ID as A_INT_CourseID","title as STR_SubjectName","triposGroupID as TXT_CourseURL"));
		}
		$papers = $data;
		
		return $papers;
	}
	
	/**
	 * Retrieve a list of the available triposes
	 * 
	 * @return array $data
	 */
	public function getTriposes() {
		$triposes = $this->select("tbl_triposGroups","*","WHERE `L_INT_EndYear` is NULL AND `BOOL_Independent` = 1");
		return $triposes;
	}
	
	/**
	 * Retrieve the current academic year
	 * 
	 * @return $resultKeys[0]
	 */
	public function getAcademicYear()
	{
		$query = "SELECT A_INT_AcademicYearID, STR_YearDescription FROM (tbl_Current_AcademicYear INNER JOIN tbl_AcademicYears ON tbl_Current_AcademicYear.L_INT_CurrentAcademicYear = tbl_AcademicYears.A_INT_AcademicYearID);";
		
		$result = odbc_exec($this->db, $query);

		$row = odbc_fetch_array($result);
		
		$rowKeys = array_keys($row);
		return $row['A_INT_AcademicYearID'];

		$resultKeys = array_keys($result);
		$currentAcademicYear = $resultKeys[0];
		
		return $currentAcademicYear;
		
	}
}
?>
